<?php
/**
 * 杭电订水service
 * Created by Sublime Text 3
 * User: wujun
 * Date: 2017/03/13 17:37
 */


class WaterService extends BaseService
{

    public $mCommunity;
    public $mBuilding;
    public $mFloor;
    public $mRoom;

    public function _initialize()
    {
        parent::_initialize();
        $this->mCommunity = D('Base/Community');
        $this->mBuilding = D('Base/Building');
        $this->mFloor = D('Base/Floor');
        $this->mRoom = D('Base/Room');

    }


    /**
     * 生成园区下拉列表
     * @param: int $accountId
     * @param int $default
     */
    public function createCommunity($accountId,$default = 0){
        $list = $this->getCommunity($accountId);
        return $this->createOption($list,$default,'请选择校区');
    }

    /**
    * 生成楼幢下拉列表
    * @param: int $accountId
    * @param int $communityId
    * @param int $default
    */
    public function createBuilding($accountId,$communityId,$default = 0){
        $list = $this->getBuilding($accountId,$communityId);
        return $this->createOption($list,$default,'请选择楼幢');
    }


    /**
    * 生成楼层下拉列表
    * @param: int $accountId
    * @param int $buildingId
    * @param int $default
    */
    public function createFloor($accountId,$buildingId,$default = 0){
        $list = $this->getFloor($accountId,$buildingId);
        return $this->createOption($list,$default,'请选择楼层');
    }


    /**
    * 生成房间下拉列表
    * @param: int $accountId
    * @param int $floorId
    * @param int $default
    */
    public function createRoom($accountId,$floorId,$default = 0){
        $list = $this->getRoom($accountId,$floorId);
        return $this->createOption($list,$default,'请选择房间');
    }

    /**
     * 园区信息
     * @param int $accountId
     */
    public function getCommunity($accountId){
        $condition = array(
            'account_id'    =>  $accountId,
            'cut_off'       =>  0
        );
        $field = 'community_id as id,community_name as name,community_code,sort,create_time';
        return $this->mCommunity->getList($condition,$field);
    }

    /**
    * 获得园区下的楼幢
    * @param int $accountId
    * @param int $communityId 园区
    */
    public function getBuilding($accountId,$communityId){
        $condition = array(
            'account_id'    =>  $accountId,
            'community_id'  =>  $communityId,
            'cut_off'       =>  0
            );
        $field = 'building_id as id,building_name as name,building_code,sort,create_time';
        return $this->mBuilding->getList($condition,$field);
    }


    /**
    * 获得楼幢下的楼层
    * @param int $accountId
    * @param int $buildingId
    */
    public function getFloor($accountId,$buildingId){
        $condition = array(
            'account_id'    =>  $accountId,
            'building_id'   =>  $buildingId,
            'cut_off'       =>  0
            );
        $field = 'floor_id as id,floor_name as name,sort,create_time';
        return $this->mFloor->getList($condition,$field);
    }


     /**
    * 获得楼幢下的楼层
    * @param int $accountId
    * @param int $buildingId
    */
    public function getRoom($accountId,$floorId){
        $condition = array(
            'account_id'    =>  $accountId,
            'floor_id'   =>  $floorId,
            'cut_off'       =>  0
            );
        $field = 'room_id as id,room_name as name,sort,create_time,building_id,floor_id,user_sum';
        return $this->mRoom->getList($condition,$field);
    }

    /**
    * 生成下拉选择公用方法
    * @param:array  $data 列表数据
    * @param:int  $default 默认值
    */
    public function createOption($data,$default=0,$optionStr='请选择'){

        $str = $default==0?'<option value="0" selected="selected">'.$optionStr.'</option>':'<option value="0">'.$optionStr.'</option>';
        foreach($data as $v){
            if($default==$v['id']){
                $str.="<option selected='selected' value='".$v['id']."'>".$v['name']."</option>";
            }else{
                $str.="<option value='".$v['id']."'>".$v['name']."</option>";
            }
            
        }
        return $str;
    }    

     /**
    * 获得地址
    * @param array $data
    * @param int $type
    */
    public function getAddr($data, $type = 0) {

        $addr = array();

        $addr['community_name'] = $this->mCommunity->where(array('community_id'=>$data['community_id']))->getField('community_name');
        $addr['building_name'] = $this->mBuilding->where(array('building_id'=>$data['building_id']))->getField('building_name');
        $addr['floor_name'] = $this->mFloor->where(array('floor_id'=>$data['floor_id']))->getField('floor_name');
        $addr['room_name'] = $this->mRoom->where(array('room_id'=>$data['room_id']))->getField('room_name');

        if ($type == 0) {
            return implode(' ', $addr);
        }
        else {
            return $addr;
        }

        
    }     

}